package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;
import artid.com.arttogoa.R;

import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.IbeaconStatisticsParser;
import artid.com.arttogoa.parser.MyArtPush_IbactivityListBean;
import artid.com.arttogoa.util.ToastUtil;
import artid.com.arttogoa.view.LineCharView;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：IbeaconStatistics_Activity   
 * 类描述   ：Ibeacon数据统计
 * 创建人   ：耿卫斌   
 * 创建时间：2016-11-15 上午11:30:40   
 *
 */
public class IbeaconStatistics_Activity extends BaseActivity implements OnClickListener{
	private TextView allBtn,monthBtn,weekBtn,deviceName_tv;
	private MyArtPush_IbactivityListBean mMyArtPush_IbactivityListBean;
	private LinearLayout noDataLL;
	private TextView noDataHint;
	private LineCharView mLineCharView;
	private List<String> x_coords;//折线图X轴数组
	private List<Integer> y_coords;//折线图Y轴数组
	private List<Integer> x_coord_values;//折线图数据


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ibeaconstatistics);
		initView();
	}

	private void initView(){
		mContext=this;
		setBack();
		setContent(this, getIntent());
		mTittle.setText("数据统计");
		mMyArtPush_IbactivityListBean=(MyArtPush_IbactivityListBean) getIntent().getSerializableExtra("MyArtPush_IbactivityListBean");
		noDataLL=(LinearLayout) findViewById(R.id.include_noDataLL);
		noDataHint=(TextView) findViewById(R.id.include_noData_hint);
		noDataHint.setText("哎呦！您暂时还有统计数据！");
		allBtn=(TextView) findViewById(R.id.IbeaconStatistics_allBtn);
		allBtn.setOnClickListener(this);
		monthBtn=(TextView) findViewById(R.id.IbeaconStatistics_monthBtn);
		monthBtn.setOnClickListener(this);
		weekBtn=(TextView) findViewById(R.id.IbeaconStatistics_weekBtn);
		weekBtn.setOnClickListener(this);
		deviceName_tv=(TextView) findViewById(R.id.IbeaconStatistics_deviceName);
		deviceName_tv.setText(mMyArtPush_IbactivityListBean.getIbeacon_ranking());
		mLineCharView=(LineCharView) findViewById(R.id.IbeaconStatistics_LineCharView);
		getData("3");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.IbeaconStatistics_allBtn:
			//全部
			changeBtnColor(v);
			break;
		case R.id.IbeaconStatistics_monthBtn:
			//月
			changeBtnColor(v);
			break;
		case R.id.IbeaconStatistics_weekBtn:
			//周
			changeBtnColor(v);
			break;

		default:
			break;
		}
	}

	/**
	 * 改变按钮背景颜色与字体颜色
	 */
	private void changeBtnColor(View v){
		switch (v.getId()) {
		case R.id.IbeaconStatistics_allBtn:
			//全部
			allBtn.setBackgroundResource(R.drawable.tongji_bg);
			allBtn.setTextColor(Color.parseColor("#ffffff"));
			monthBtn.setBackgroundResource(0);
			monthBtn.setTextColor(Color.parseColor("#9C9C9C"));
			weekBtn.setBackgroundResource(0);
			weekBtn.setTextColor(Color.parseColor("#9C9C9C"));
			getData("3");
			break;
		case R.id.IbeaconStatistics_monthBtn:
			//月
			monthBtn.setBackgroundResource(R.drawable.tongji_bg);
			monthBtn.setTextColor(Color.parseColor("#ffffff"));
			allBtn.setBackgroundResource(0);
			allBtn.setTextColor(Color.parseColor("#9C9C9C"));
			weekBtn.setBackgroundResource(0);
			weekBtn.setTextColor(Color.parseColor("#9C9C9C"));
			getData("2");
			break;
		case R.id.IbeaconStatistics_weekBtn:
			//周
			weekBtn.setBackgroundResource(R.drawable.tongji_bg);
			weekBtn.setTextColor(Color.parseColor("#ffffff"));
			allBtn.setBackgroundResource(0);
			allBtn.setTextColor(Color.parseColor("#9C9C9C"));
			monthBtn.setBackgroundResource(0);
			monthBtn.setTextColor(Color.parseColor("#9C9C9C"));
			getData("1");
			break;

		default:
			break;
		}
	}

	/**
	 * 获取统计数量
	 * @param type
	 * 1为按天，2为按月，3为按年
	 */
	private void getData(String type){
		VolleyBasic.getInstance((Activity)mContext).post(Cache.ibeaconCount,
				VolleyMap.getInstance().ibeaconCount(mMyArtPush_IbactivityListBean.getIbeaconid(),type),
				IbeaconStatisticsParser.class, new VolleyCallback() {

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				List<IbeaconStatisticsParser> list=(List<IbeaconStatisticsParser>) vd.getList();
				Log.d("log", "统计数据："+list.toString());
				x_coords=new ArrayList<String>();
				x_coord_values=new ArrayList<Integer>();
				y_coords=new ArrayList<Integer>();
				int maxNum=0;//最大数
				int baseNum=1;//基数
				if(list!=null && list.size()!=0){
					for (int i = 0; i < list.size(); i++) {
						x_coords.add(list.get(i).getTime());
						x_coord_values.add(list.get(i).getNum());
						//这个是为了防止最大值超过限制做的处理
						if(list.get(i).getNum()>maxNum){
							maxNum=list.get(i).getNum();
						}
					}
					if(maxNum>10 && maxNum<=100){
						baseNum=10;
					}else if(maxNum>100 && maxNum<=300){
						baseNum=30;
					}else if(maxNum>300 && maxNum<=600){
						baseNum=60;
					}else if(maxNum>600 && maxNum<=1000){
						baseNum=100;
					}else if(maxNum>1000 && maxNum<=3000){
						baseNum=300;
					}else if(maxNum>3000 && maxNum<=6000){
						baseNum=600;
					}else if(maxNum>3000 && maxNum<=10000){
						baseNum=1000;
					}
					//根据最大值添加Y轴数据
					y_coords.add(0);
					y_coords.add(2*baseNum);
					y_coords.add(4*baseNum);
					y_coords.add(6*baseNum);
					y_coords.add(8*baseNum);
					y_coords.add(10*baseNum);
					mLineCharView.setBgColor(Color.WHITE);
					mLineCharView.setValue(x_coords, y_coords,x_coord_values);
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
				}
			}

			public void onError(VolleyError volleyError) {
				noDataLL.setVisibility(View.VISIBLE);
				ToastUtil.toast(mContext, "请求失败，请重试");
			}
		});
	}
}
